package com.mhc.haval.controller;

import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.form.DealerAccountForm;
import com.mhc.haval.form.DealerDetilForm;
import com.mhc.haval.form.DealerQueryForm;
import com.mhc.haval.security.model.User;
import com.mhc.haval.vo.DealerAccountVO;
import com.mhc.haval.vo.DealerDetailVO;
import com.mhc.haval.vo.DealerInfoVO;
import com.mhc.suzuki.api.model.DealerAccountDTO;
import com.mhc.suzuki.api.model.DealerDetailDTO;
import com.mhc.suzuki.api.model.DealerInfoDTO;
import com.mhc.suzuki.api.model.DealerQueryDTO;
import com.mhc.suzuki.api.service.IDealerService;
import com.subaru.common.entity.BizResult;
import com.subaru.common.entity.ErrorCodeEnum;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by HW
 * on 2017/9/14 21:27.
 * 经销商管理接口
 */

@Slf4j
@RestController
@RequestMapping(value = "/dealer")
public class DealerController {

    @Autowired
    private IDealerService iDealerService;

    @ApiOperation(value = "经销商列表查看", notes = "所有经销商获取")
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public BizResult<List<DealerInfoVO>> getDealerList(@AuthenticationPrincipal User user, DealerQueryForm dealerQueryForm) {

        Optional<User> userOptional = Optional.ofNullable(user);
        if (userOptional.isPresent()){
            DealerQueryDTO dealerQueryBO = BeanCopierUtil.convert(dealerQueryForm, DealerQueryDTO.class);
            dealerQueryBO.setCompanyNo(user.getCompanyNo());

            //查询经销商列表
            List<DealerInfoDTO> dealerInfoDTOS = iDealerService.getDealerInfoDOS(dealerQueryBO);
            //查询总数
            int count = iDealerService.getCountByQuery(dealerQueryBO);

            /*BO->VO*/
            List<DealerInfoVO> dealerInfoVOS = new ArrayList<>();
            for (DealerInfoDTO dealerInfoDTO : dealerInfoDTOS) {
                DealerInfoVO dealerInfoVO = BeanCopierUtil.convert(dealerInfoDTO, DealerInfoVO.class);
                dealerInfoVOS.add(dealerInfoVO);
            }

            BizResult<List<DealerInfoVO>> bizResult = BizResult.create(dealerInfoVOS, true, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
            bizResult.setPageNo(dealerQueryForm.getPageNo());
            bizResult.setPageSize(dealerQueryForm.getPageSize());
            bizResult.setTotalCount(count);
            return bizResult;
        } else {
            return BizResult.create(ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrCode(), ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrMsg());
        }




    }





    /**
     * 根据经销商id查询经销商具体信息
     * @param dealerId
     * @return
     */
    @ApiOperation(value = "经销商详细信息查看", notes = "获取经销商详细信息")
    @RequestMapping(value = {"/detail/{dealerId}"}, method = RequestMethod.GET)
    public BizResult<DealerDetailVO> getDealerDetail(@PathVariable Long dealerId){
        //查询操作
        DealerDetailDTO dealerDetailBO = iDealerService.getDealerDetail(dealerId);
        DealerDetailVO dealerDetailVO = BeanCopierUtil.convert(dealerDetailBO, DealerDetailVO.class);
        List<DealerAccountVO> dealerAccountVOS = new ArrayList<>();
        for(DealerAccountDTO dealerAccountBO : dealerDetailBO.getDealerAccountDTOS()){
            DealerAccountVO dealerAccountVO = BeanCopierUtil.convert(dealerAccountBO, DealerAccountVO.class);
            dealerAccountVOS.add(dealerAccountVO);
        }
        dealerDetailVO.setDealerAccountVOS(dealerAccountVOS);
        return BizResult.create(dealerDetailVO, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

    }




    /**
     * 根据经销商id删除对应经销商信息
     * @param dealerId
     * @return
     */
    @ApiOperation(value = "经销商删除", notes = "删除对应经销商")
    @RequestMapping(value = {"/{dealerId}"}, method = RequestMethod.DELETE)
    public BizResult deleteDealer(@PathVariable Long dealerId){
        if(dealerId != null && iDealerService.deleteDealerById(dealerId)){
            return BizResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        }
        return BizResult.create(ErrorCodeEnum.PARAM_ERROR.getErrCode(), ErrorCodeEnum.PARAM_ERROR.getErrMsg());

    }




    /**
     * 添加经销商
     * @param dealerDetilForm 经销商详细信息
     * @return
     */
    @ApiOperation(value = "经销商添加", notes = "添加对应经销商信息")
    @RequestMapping(value = {"/newDealer"}, method = RequestMethod.POST)
    public BizResult addDealer(@RequestBody DealerDetilForm dealerDetilForm, @AuthenticationPrincipal User user){

        Optional<User> userOptional = Optional.ofNullable(user);

        if(userOptional.isPresent()){
            /* FORM -> DTO */
            DealerDetailDTO dealerDetailDTO = BeanCopierUtil.convert(dealerDetilForm, DealerDetailDTO.class);
            dealerDetailDTO.setCompanyNo(user.getCompanyNo());
            List<DealerAccountDTO> dealerAccountDTOS = new ArrayList<>();
            for(DealerAccountForm dealerAccountForm : dealerDetilForm.getDealerAccountForms()){
                DealerAccountDTO dealerAccountDTO = BeanCopierUtil.convert(dealerAccountForm, DealerAccountDTO.class);
                dealerAccountDTOS.add(dealerAccountDTO);
            }
            dealerDetailDTO.setDealerAccountDTOS(dealerAccountDTOS);
            if (iDealerService.addDealer(dealerDetailDTO)){
                return BizResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
            } else {
                return BizResult.create(ErrorCodeEnum.INSERT_FAIL.getErrCode(), ErrorCodeEnum.INSERT_FAIL.getErrMsg());
            }
        } else {
            return BizResult.create(ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrCode(), ErrorCodeEnum.LOGIN_ESIGN_ERROR.getErrMsg());
        }
    }




    /**
     * 更新经销商
     * @param dealerDetilForm 经销商详细信息
     * @return
     */
    @ApiOperation(value = "经销商更新", notes = "更新对应经销商信息")
    @RequestMapping(value = {"/newDealer"}, method = RequestMethod.PUT)
    public BizResult updateDealer(@RequestBody DealerDetilForm dealerDetilForm){

        /* FORM -> DTO */
        DealerDetailDTO dealerDetailDTO = BeanCopierUtil.convert(dealerDetilForm, DealerDetailDTO.class);
        List<DealerAccountDTO> dealerAccountDTOS = new ArrayList<>();
        for(DealerAccountForm dealerAccountForm : dealerDetilForm.getDealerAccountForms()){
            DealerAccountDTO dealerAccountDTO = BeanCopierUtil.convert(dealerAccountForm, DealerAccountDTO.class);
            dealerAccountDTO.setDealerId(dealerDetailDTO.getDealerId());
            dealerAccountDTOS.add(dealerAccountDTO);
        }
        dealerDetailDTO.setDealerAccountDTOS(dealerAccountDTOS);
        if (iDealerService.updateDealer(dealerDetailDTO)){
            return BizResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } else {
            return BizResult.create(ErrorCodeEnum.UPDATE_FAIL.getErrCode(), ErrorCodeEnum.UPDATE_FAIL.getErrMsg());
        }
    }



}
